//
//  CantripColorManager.m
//  Color changer window controller
//
//  Created by James Callender on Sat Jul 19 2003.
//  Copyright (c) 2003 James Callender. All rights reserved.
//

#import "CantripColorManager.h"

static NSString *ansiNames[8] = {
        @"Black",
        @"White",
        @"Green",
        @"Red",
        @"Magenta",
        @"Yellow",
        @"Cyan" };

@implementation CantripColorManager

+ (CantripColorManager *)sharedInstance
{
    static CantripColorManager *sharedInstance = nil;

    if (!sharedInstance)
    {
        sharedInstance = [[self alloc] init];
    }
    
    return sharedInstance;
}

- (id)init
{
    if (self = [super init])
    {
        standardString = [[NSString stringWithString:@"Standard"] retain];
        boldString = [[NSString stringWithString:@"Bold"] retain];
        colorPairs = [[NSMutableArray alloc] initWithObjects:
            [NSArray arrayWithObjects:@"StandardBlack", @"Black", nil],
            [NSArray arrayWithObjects:@"StandardWhite", @"White", nil],
            [NSArray arrayWithObjects:@"StandardGreen", @"Green", nil],
            [NSArray arrayWithObjects:@"StandardRed", @"Red", nil],
            [NSArray arrayWithObjects:@"StandardMagenta", @"Magenta", nil],
            [NSArray arrayWithObjects:@"StandardYellow", @"Yellow", nil],
            [NSArray arrayWithObjects:@"StandardCyan", @"Cyan", nil],
            [NSArray arrayWithObjects:@"BoldBlack", @"Black", nil],
            [NSArray arrayWithObjects:@"BoldWhite", @"White", nil],
            [NSArray arrayWithObjects:@"BoldGreen", @"Green", nil],
            [NSArray arrayWithObjects:@"BoldRed", @"Red", nil],
            [NSArray arrayWithObjects:@"BoldMagenta", @"Magenta", nil],
            [NSArray arrayWithObjects:@"BoldYellow", @"Yellow", nil],
            [NSArray arrayWithObjects:@"BoldCyan", @"Cyan", nil],
            nil];
        colorsDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
            [NSColor colorWithCalibratedWhite:0.4 alpha:1.0], @"StandardBlack",
            [NSColor colorWithCalibratedWhite:0.7 alpha:1.0], @"StandardWhite",
            [NSColor colorWithCalibratedRed:0.0 green:0.7 blue:0.0 alpha:1.0], @"StandardGreen",
            [NSColor colorWithCalibratedRed:0.7 green:0.0 blue:0.0 alpha:1.0], @"StandardRed",
            [NSColor colorWithCalibratedRed:0.7 green:0.0 blue:0.7 alpha:1.0], @"StandardMagenta",
            [NSColor colorWithCalibratedRed:0.7 green:0.7 blue:0.0 alpha:1.0], @"StandardYellow",
            [NSColor colorWithCalibratedRed:0.0 green:0.7 blue:0.7 alpha:1.0], @"StandardCyan",
            
            [NSColor colorWithCalibratedWhite:0.2 alpha:1.0], @"BoldBlack",
            [NSColor colorWithCalibratedWhite:0.9 alpha:1.0], @"BoldWhite",
            [NSColor greenColor], @"BoldGreen",
            [NSColor redColor], @"BoldRed",
            [NSColor magentaColor], @"BoldMagenta",
            [NSColor yellowColor], @"BoldYellow",
            [NSColor cyanColor], @"BoldCyan",
            nil];
    }
    
    return self;
}

- (void)dealloc
{
    if (self != [self sharedInstance]) 
    {
        [standardString release];
        [boldString release];
        [colorsDict release];
        [colorPairs release];
        [super dealloc];
    }
}

- (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
{
    if (item == nil)
    {
        if (index == 0) return standardString;
        if (index == 1) return boldString;
    }
    
    if ([item isEqual:@"Standard"])
    {
        return [colorPairs objectAtIndex:index];
    }
    else if ([item isEqual:@"Bold"])
    {
        return [colorPairs objectAtIndex:index + 7];
    }
    
    return nil;
}

- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
{
    if ([item isEqual:@"Standard"]) return YES;
    if ([item isEqual:@"Bold"]) return YES;
    
    return NO;
}

- (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{
    if (item == nil) return 2;
    if ([item isEqual:@"Standard"] || [item isEqual:@"Bold"]) return 7;
    return 0;
}

- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
{
    if ([[tableColumn identifier] isEqual:@"name"])
    {
        if ([item isEqual:@"Standard"]) return @"Standard";
        if ([item isEqual:@"Bold"]) return @"Bold";
        
        return [item objectAtIndex:1];
    }
    else if ([[tableColumn identifier] isEqual:@"color"])
    {
        if ([item isEqual:@"Standard"]) return nil;
        if ([item isEqual:@"Bold"]) return nil;
        
        return [colorsDict objectForKey:[item objectAtIndex:0]];
    }
}

- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item
{
    NSColorPanel *panel;
    clickedItem = item;
    if ([clickedItem isEqual:@"Standard"] || [clickedItem isEqual:@"Bold"]) return YES;
    panel = [NSColorPanel sharedColorPanel];
    [panel setTarget:self];
    [panel setAction:@selector(changeColor:)];
    [panel setColor:[colorsDict objectForKey:[clickedItem objectAtIndex:0]]];
    //[panel makeKeyAndOrderFront:self];
    return YES;
}

- (IBAction)showPanel:(id)sender
{
    if (!colorsView)
    {
        NSTableColumn *column;
        CantripColorCell *colorCell;
        if (![NSBundle loadNibNamed:@"ANSIColorManager" owner:self])  {
            NSLog(@"Failed to load ANSIColorManager.nib");
            return;
        }
        
        [colorsView setDataSource:self];
        [colorsView setDelegate:self];
        column = [colorsView tableColumnWithIdentifier:@"color"];
        colorCell = [[[CantripColorCell alloc] init] autorelease];
        [colorCell setEditable:YES];
        [colorCell setTarget:self];
        [colorCell setAction:@selector(clickColor:)];
        [column setDataCell:colorCell];
    }
    
    //[[colorsView window] setTitle:(windowTitle ? windowTitle : @"Colors")];
    [[colorsView window] makeKeyAndOrderFront:nil];
}

- (void)clickColor:(id)sender
{
    [[NSColorPanel sharedColorPanel] makeKeyAndOrderFront:self];
}

- (void)changeColor:(id)sender
{
    id theItem;
    theItem = clickedItem;//[sender itemAtRow:[sender clickedRow]];
     
    if ([theItem isEqual:@"Standard"] || [theItem isEqual:@"Bold"]) return;
    [colorsDict setObject:[sender color] forKey:[theItem objectAtIndex:0]]; 
    [colorsView reloadData];
}

- (void)setTitle:(NSString *)title
{
    if ([colorsView window]) [[colorsView window] setTitle:title];
}

- (NSPanel *)panel
{
    return [colorsView window];
}

@end
